************************************************
***Title: weather_clean.do
***Creators: Joelle Abramowitz, Shooshan Danagoulian, and Owen Fleming*
***Notes: This data cleans the weather data from PRISM and constructs a county-date panel.

*For questions, contact
*Owen Fleming
*hg3490@wayne.edu
************************************************


**********SETUP
cd ${path}/data/weather
clear all
set more off


**********IMPORT AND CLEAN
foreach batch in 1 2 3 4 5 6 7 {
foreach year in 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 {

import delimited batch`batch'/PRISM_ppt_tmin_tmean_tmax_tdmean_vpdmin_vpdmax_stable_4km_`year'0101_`year'1231.csv, clear

drop if inrange(_n,1,11)
rename (v1 v5 v6 v7 v8 v9 v10 v11 v12) (county date_temp ppt tmin tmean tmax tdmean vpdmin vpdmax)

gen year = substr(date_temp,1,4)
gen month = substr(date_temp,6,2)
gen day = substr(date_temp,9,2)
destring year month day county ppt tmin tmean tmax tdmean vpdmin vpdmax, force replace
gen date = mdy(month, day, year)
format date %td

keep county date ppt tmin tmean tmax tdmean vpdmin vpdmax
order county date ppt tmin tmean tmax tdmean vpdmin vpdmax

save weather_batch`batch'_`year', replace

}
}


**********APPEND FILES
clear
foreach batch in 1 2 3 4 5 6 7 {
foreach year in 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 {
append using weather_batch`batch'_`year'
}
}


**********SAVE
save weather, replace


**********REVERT DIRECTORY
cd ${path}





